package org.swimv2.servlets;

import java.io.IOException;
import java.util.List;

import javax.naming.Context;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.swimv2.entities.Skill;
import org.swimv2.entities.User;
import org.swimv2.interfaces.SearchManagerRemote;
import org.swimv2.interfaces.SkillManagerRemote;
import org.swimv2.utils.ContextUtil;

/**
 * Servlet implementation class SearchUsers
 */
public class SearchUsersServlet extends HttpServlet {

	private static final long serialVersionUID = -5183743393837467366L;

	
	private SearchManagerRemote     mgr_u = null;
	private SkillManagerRemote      mgr_s = null;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public SearchUsersServlet() {
        super();
    }

	/**
	 * Visualizza la pagina di ricerca
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		try {
			Context ctx = ContextUtil.getInitialContext();
			mgr_u = (SearchManagerRemote)     ctx.lookup("SearchManagerJNDI");
			mgr_s = (SkillManagerRemote)      ctx.lookup("SkillManagerJNDI");
			
			RequestDispatcher disp;
			disp = request.getRequestDispatcher("app/search.jsp");
			// Aggiunge la lista di tutte le skill
			request.setAttribute("skills", (List<Skill>) mgr_s.listAllActiveSkills());
			// Effettua il redirect
			disp.forward(request, response);
		}
		catch (NamingException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Effettua la ricerca e visualizza il risultato
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		
		try {
			Context ctx = ContextUtil.getInitialContext();
			mgr_u = (SearchManagerRemote)     ctx.lookup("SearchManagerJNDI");
			mgr_s = (SkillManagerRemote)      ctx.lookup("SkillManagerJNDI");
			
			RequestDispatcher disp;

			String par_how = request.getParameter("how");
			/* Cerca per nome */
			if (par_how.equals("NAME")) {
				String par_name = request.getParameter("name");
				List<User> uList = mgr_u.searchForName(par_name);
				request.setAttribute("users_result", uList);
				
				request.setAttribute("message", "Ricerca effettuata.");
			}
			/* Cerca per competenza */
			else if(par_how.equals("SKILL")) {
				String par_skill = request.getParameter("skill");
				Skill s = mgr_s.getSkill(Integer.parseInt(par_skill));
				List<User> uList = mgr_u.searchForSkill(s);
				request.setAttribute("users_result", uList);
				
				request.setAttribute("message", "Ricerca effettuata.");
			}
			
			// Aggiunge la lista di tutte le skill
			request.setAttribute("skills", (List<Skill>) mgr_s.listAllActiveSkills());
			// Effettua il redirect
			disp = request.getRequestDispatcher("app/search.jsp");
			disp.forward(request, response);
		}
		catch (NamingException e) {
			e.printStackTrace();
		}
	}

}
